package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.ChannelAreaService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.constant.Const;
import com.iot08.common.entity.admin.ChannelArea;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2020/12/7 11:45
 */
@Controller
@RequestMapping("channelArea")
public class ChannelAreaController extends BaseController {

    @Resource
    private ChannelAreaService channelAreaService;

    /**
     * @param page
     * @time: 2020/12/7 15:04
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        if (user.getType() == Const.CHANNELUSER_TYPE && user.getChannelArea() != null &&
                user.getChannelArea().getLevel() > 1) {
            mv.addObject("msg", "无权限查看");
            mv.setViewName("save_result");
            return mv;
        }
        Page<ChannelArea> pages = channelAreaService.list(page, this.getSessionUser());
        mv.addObject("channelAreas", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.addObject("user", user);
        mv.setViewName("channelArea/channel_area_list");
        return mv;
    }

    /**
     * 添加页面
     *
     * @time: 2020/12/7 15:04
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        List<ChannelArea> channelAreas = new ArrayList<>();
        if (user.getType() == Const.SYSUSER_TYPE) {
            channelAreas = channelAreaService.listByLevel(0, user, false);
        } else {
            //获取当前账号区域级别
            int areaLevel = user.getChannelArea() == null ? 1 : user.getChannelArea().getLevel();
            //获取当前账号渠道归属内的渠道列表
            List<Long> channelCodes = new ArrayList<>();
            if (areaLevel > 1) {
                channelCodes.add(user.getChannelArea().getChannelCode());
            } else {
                channelCodes = user.getChannelAff().getChannelCodes();
            }
            channelAreas = channelAreaService.listByLevel(areaLevel - 1, user, false);
        }
        mv.addObject("channelAreas", channelAreas);
        mv.setViewName("channelArea/channel_area_add");
        return mv;
    }

    /**
     * 添加
     *
     * @param channelArea
     * @time: 2020/12/7 14:45
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(ChannelArea channelArea) {
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        channelArea.setCreator(user.getId());
        channelAreaService.save(channelArea, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 编辑页面
     *
     * @param id
     * @param showType
     * @time: 2020/12/7 15:04
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        ChannelArea channelArea = channelAreaService.findById(id);
        if (channelArea != null && StringUtil.isNotBlank(channelArea.getPid())) {
            ChannelArea parea = channelAreaService.findById(channelArea.getPid());
            if (parea != null) {
                channelArea.setPname(parea.getName());
            }
        }
        mv.addObject("channelArea", channelArea);
        mv.setViewName("channelArea/channel_area_edit");
        return mv;
    }

    /**
     * 保存
     *
     * @param channelArea
     * @time: 2020/12/7 15:04
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(ChannelArea channelArea) {
        ModelAndView mv = new ModelAndView();
        channelAreaService.update(channelArea, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 删除
     *
     * @param id
     * @time: 2020/12/7 15:05
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public Response<Object> del(String id) {
        return channelAreaService.updateDel(id, this.getSessionUser(), this.getRemortIP());
    }

}
